package com.atguigu.mr.combiner;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @description: 在mapTask上运行combine, 对每一个mapTask进行局部汇总, 以减少网络流量,另外一般这个类不需要写,直接使用自身的Reducer即可
 * @time: 20191226 20:34
 * @author: baojinlong
 **/
public class WordcountCombiner extends Reducer<Text, IntWritable, Text, IntWritable> {
    IntWritable v = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        // 1 汇总
        int sum = 0;
        for (IntWritable value : values) {
            sum += value.get();
        }
        v.set(sum);
        // 2 写出
        context.write(key, v);
    }
}